#include <QFileDialog>
#include <QDir>
#include <QDebug>

#include "iqtprojectgenerator.h"
#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include "settings.h"

SettingsDialog::SettingsDialog(IQtProjectGenerator * generator,
                               const QString       & widgetFile,
                               QWidget             * parent)
	: QDialog(parent),
        m_generator(generator),
        m_platformGui(NULL),
	ui(new Ui::SettingsDialog)
{
	ui->setupUi(this);

    m_platformGui = m_generator->createSettingsWidget(widgetFile,
                                                      this);
    ui->settingsFrame->layout()->addWidget(m_platformGui);

    m_platformPlugins = m_generator->createSettingsPluginsWidget(this);
    ui->pluginsFrame->layout()->addWidget(m_platformPlugins);

	setWindowTitle("Settings");
	setModal(true);
	connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(save()));

        /* OBS FDOS
	// Read values
	ui->uid3Value->setText(Settings::get(Settings::UID3).toString());
	ui->qtenvValue->setText(Settings::get(Settings::QtEnvBatPath).toString()); // Note: check for Qt before mobility path! Mobility tries to guess based on Qt path.
	ui->logFileValue->setText(Settings::get(Settings::LogFilePath).toString());
	ui->layoutValue->setCurrentIndex(Settings::get(Settings::WidgetLayout).toInt());
	ui->checkWidgetPannable->setChecked(Settings::get(Settings::PanningEnabled).toBool());
	ui->checkTextSelectable->setChecked(Settings::get(Settings::TextSelectionEnabled).toBool());
	if (Settings::get(Settings::MobilityIsTechnologyPreview).toBool()) // Uses Mobility path, so check that first
	{
		ui->radioMobilityPreview->setChecked(true);
	}
	else
	{
		ui->radioMobilityBeta->setChecked(true);
	}

	// Connect browse buttons
	connect(ui->buttonBrowseBat, SIGNAL(clicked()), this, SLOT(browseForBat()));
	connect(ui->buttonBrowseLog, SIGNAL(clicked()), this, SLOT(browseForLog()));
        */
}

SettingsDialog::~SettingsDialog()
{
	delete ui;
}

void SettingsDialog::save()
{
    /* OBS FDOS
	Settings::set(Settings::UID3, ui->uid3Value->text().trimmed());
	Settings::set(Settings::QtEnvBatPath, ui->qtenvValue->text().trimmed());
	Settings::set(Settings::LogFilePath, ui->logFileValue->text().trimmed());
	Settings::set(Settings::WidgetLayout, ui->layoutValue->currentIndex());
	Settings::set(Settings::PanningEnabled, ui->checkWidgetPannable->isChecked());
	Settings::set(Settings::TextSelectionEnabled, ui->checkTextSelectable->isChecked());
	Settings::set(Settings::MobilityIsTechnologyPreview, ui->radioMobilityPreview->isChecked());
     */

    m_generator->saveSettings(m_platformGui);
    m_generator->savePluginSettings(m_platformPlugins);
}

/* OBS FDOS
void SettingsDialog::browseForBat()
{
	QFileInfo file = getFileInfo(Settings::get(Settings::QtEnvBatPath).toString());
	QString fileName = QFileDialog::getOpenFileName(this, tr("Select qtenv.bat"), file.absolutePath(), tr("Batch files") + " (*.bat);;" + tr("All Files (*)"));
	if (!fileName.isEmpty())
	{
		fileName = QDir::toNativeSeparators(fileName);
		ui->qtenvValue->setText(fileName);
	}
}

void SettingsDialog::browseForLog()
{
	// Existing
	QFileInfo file = getFileInfo(Settings::get(Settings::LogFilePath).toString());
	QString fileName = QFileDialog::getOpenFileName(this, tr("Select application log file"), file.absolutePath(), tr("Log files") + " (*.log);;" + tr("All Files (*)"));
	if (!fileName.isEmpty())
	{
		fileName = QDir::toNativeSeparators(fileName);
		ui->logFileValue->setText(fileName);
	}
}

/ **
 * Returns a FileInfo object created of the given file. If the file's
 * directory does not exist, returns a FileInfo of root directory instead.
 * /
QFileInfo SettingsDialog::getFileInfo(QString fileName)
{
	QFileInfo file(fileName);
	if (!QFile::exists(file.absolutePath()))
	{
		file = QFileInfo(QDir::root().absolutePath());
	}
	return file;
}
OBS */
